from aip import AipFace
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait

from config import *
from baidu_aip_utils import *


# 百度AIP接口
client = AipFace(BAIDU_AIP_APP_ID, BAIDU_AIP_API_KEY, BAIDU_AIP_SECRET_KEY)


"""Chrome驱动配置"""
chrome_option = webdriver.ChromeOptions()

driver = webdriver.Chrome(executable_path=CHROME_DRIVER_PATH)
driver.get("https://www.zhenai.com")

# 注入cookie，使登录生效
for cookie_name, cookie_value in COOKIES_ZHENAI.items():
    driver.add_cookie({"name": cookie_name, "value": cookie_value})

driver.get("https://www.zhenai.com/n/search")

# 等待脚本加载完成，元素可以发现
WebDriverWait(driver, 10).until(lambda x: x.find_elements_by_class_name("avatar-box"))


"""待匹配的图片数据"""
image_urls = []
meizi_elements = driver.find_elements_by_class_name("avatar-box")

# 像Javascript一样操作Dom节点
for element in meizi_elements:
    url = element.value_of_css_property("background-image").lstrip("url(\"").rstrip("\")")
    # 找到了图片地址
    if url and url.startswith("http"):
        image_urls.append(url)
driver.quit()


""" 调用人脸比对 """
if __name__ == '__main__':
    if image_urls:
        for meizi_image_url in image_urls:
            # 调用人脸比对
            images = [
                image_file_format('data/ashuang.jpg'),
                http_image_format(meizi_image_url)
            ]
            result_json = client.match(images)
            print(result_json)
